/*
 * GContestSeasonLink.java
 * Created on 6 sept. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.contest;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.season.GSeason;

/**
 * Represents the link between a contest and the seasons in which it takes place.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "h_contest_season_link" )
public class GContestSeasonLink implements IGEntity
{
  @Id
  @Column( name = "id" )
  @GeneratedValue( strategy = GenerationType.AUTO )
  private int      d_nId;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "contest_id" )
  private GContest d_contest;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "first_season_id" )
  private GSeason  d_seasonFirst;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "last_season_id" )
  private GSeason  d_seasonLast;

  /** Default no-argument constructor. */
  protected GContestSeasonLink()
  {}

  public GContest getContest()
  {
    return d_contest;
  }

  public GSeason getFirstSeason()
  {
    return d_seasonFirst;
  }

  public GSeason getLastSeason()
  {
    return d_seasonLast;
  }

  public int getId()
  {
    return d_nId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GContestSeasonLink.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GContestSeasonLink) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return String.valueOf( d_nId ) + " (contest " + d_contest.getId() + " from season " + ( d_seasonFirst != null
        ? d_seasonFirst.getId() : "n/a" ) + " to " + ( d_seasonLast != null
        ? d_seasonFirst.getId() : "n/a" ) + ")";
  }
}
